package com.xyht.sca_s.student_manage_system.modules.advanceRegistration.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.annotation.RequestPass;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.advanceRegistration.entity.RxAdvanceRegistrationOrg;
import com.xyht.sca_s.student_manage_system.modules.advanceRegistration.entity.req.RxAdvanceRegistrationOrgReq;
import com.xyht.sca_s.student_manage_system.modules.advanceRegistration.service.RxAdvanceRegistrationOrgService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.concurrent.Future;

/**
 * <p>
 * 预报名组织架构 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2024-04-10
 */
@RestController
@RequestMapping("/advanceRegistration/org")
@SecretBody
public class RxAdvanceRegistrationOrgController {

    @Resource
    private RxAdvanceRegistrationOrgService advanceRegistrationOrgService;
    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @ApiOperation(value = "专业批量导入")
    @PostMapping("/batch/add")
    public ResponseResult batchAdvanceRegistrationOrg(@RequestBody Map<String,Object> map, HttpServletRequest request){
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        ResponseResult commonCodeEnum;
        try{
            Future<ResponseResult> future = advanceRegistrationOrgService.batchAdvanceRegistrationOrg(map,user_id);
            while (true){
                if (future.isDone()){
                    commonCodeEnum = future.get();
                    break;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return commonCodeEnum;
    }

    @ApiOperation("添加专业")
    @PostMapping("/add")
    public ResponseResult addAdvanceRegistrationOrg(@RequestBody RxAdvanceRegistrationOrgReq addReq, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return advanceRegistrationOrgService.addAdvanceRegistrationOrg(addReq, userId);
    }

    @ApiOperation("删除专业")
    @PostMapping("/deleted")
    public ResponseResult deletedAdvanceRegistrationOrg(@RequestBody RxAdvanceRegistrationOrgReq deletedReq) {
        return advanceRegistrationOrgService.deletedAdvanceRegistrationOrg(deletedReq);
    }

    @ApiOperation("修改专业")
    @PostMapping("/update")
    public ResponseResult updateAdvanceRegistrationOrg(@RequestBody RxAdvanceRegistrationOrgReq updateReq) {
        return advanceRegistrationOrgService.updateAdvanceRegistrationOrg(updateReq);
    }


    @ApiOperation("查看专业列表")
    @GetMapping("/list")
    @RequestPass
    public ResponseResult getAdvanceRegistrationOrgList(@RequestParam(value = "searchStr", required = false) String searchStr,
                                                        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                        @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        return advanceRegistrationOrgService.getAdvanceRegistrationOrgList(pageNum, pageSize, searchStr);
    }

}

